package com.sunny.l75;

public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();
        int[][] matrix = {{1,3}};
        int target = 1;
        System.out.println(solution.searchMatrix(matrix, target));

    }

    public boolean searchMatrix(int[][] matrix, int target) {
        if(null == matrix)
            return false;
        int n = matrix.length;
        if(0 == n)
            return false;
        int m = matrix[0].length;
        int x = 0;
        int y = m-1;
        while (judge(x, y, n, m) && matrix[x][y] != target){
//            System.out.println(x+" "+y+" "+matrix[x][y]+" "+target);
            if(matrix[x][y] > target){
                y -= 1;
            }else if(matrix[x][y] < target){
                x += 1;
            }
        }
        return judge(x,y,n,m);
    }
    public boolean judge(int x, int y, int n, int m){
        if(x < 0 || x >= n || y < 0 || y >= m){
            return false;
        }
        return true;
    }
}